<template>
  <el-checkbox-group v-model="user.userResource.companies">
    <el-checkbox style="width: 25%;margin: 5px 0;" v-for="company in companies" :label="company.id" :key="company.id" @change="update($event,company)">{{company.companyName}}</el-checkbox>
  </el-checkbox-group>
</template>

<script>
  import { mapActions} from 'vuex'
  export default {
    name: "selCompanies",
    props: ['user'],
    data(){
      return {
        companies:[]
      }
    },
    created(){
      console.log(this.user)
      //获取所有公司列表
      this.getCompanyList()
    },
    methods: {
      ...mapActions({
        fetch: 'fetch'
      }),
      getCompanyList(){
        this.fetch({name:"getCompanyList"})
          .then(res=>{
            this.companies = res
            console.log(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      },
      update(e,c){
        console.log(e,c)
        let value;
        if(e) value = 1
        else value = 0
        this.fetch({name:"setUserCompany",data:{
            companyId:c.id,
            type:value,
            userId:this.user.id
          }})
          .then(res=>{
            console.log(res)
          })
          .catch(err=>{
            console.log(err)
            errHandler.handle(this,err)
          })
      }
    }
  }
</script>

<style scoped>

</style>
